package com.alibaba.citrus.cr.controller;

import com.alibaba.fastjson.JSON;
import com.epoch.app.crplatformenhance.dictionarysave.service.DictionarySaveService;
import com.epoch.app.crplatformenhance.model.dto.CreateDictValueRequest;
import com.epoch.app.crplatformenhance.model.dto.CreateDictionaryRequest;

import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @description:
 * @author: qianlei
 * @create: 2021/10/17 17:42
 */
@RestController
public class TestController {
    @Resource
    DictionarySaveService dictionarySaveService;
    @PostMapping("/test")
    public void test(@RequestBody List<CreateTest> list) {
        System.out.println("list");
    }

    //    @RequestMapping(value = "/createDict", method = RequestMethod.GET)
    @GetMapping("/createDict")
    public void batchCreate(@RequestParam String enums) {

        String text1 = "[{\"code\":\"forecastTag\",\"groupCode\":\"forecast\",\"name\":\"预报单类型\",\"map\":{\"01\":\"标准预报订单\",\"02\":\"车销预报订单\",\"03\":\"补货预报订单\",\"04\":\"共享仓直发客户预报订单\"}},{\"code\":\"saleOrderTag\",\"groupCode\":\"saleOrder\",\"name\":\"销售单类型\",\"map\":{\"ZOR2\":\"标准销售订单\",\"ZFD2\":\"免费销售订单\",\"ZXC2\":\"车销销售订单\",\"ZFD3\":\"补货销售订单\",\"ZBV\":\"现金销售订单\"}},{\"code\":\"orderReason\",\"groupCode\":\"saleOrder\",\"name\":\"单据原因\",\"map\":{\"001\":\"客户加单\",\"002\":\"奶酪订单-内销订单\",\"003\":\"按正常销售发放试饮品\",\"004\":\"客户退货\",\"005\":\"价格主数据维护有误\",\"006\":\"货损\",\"007\":\"买赠\",\"A01\":\"过期品退货\",\"A02\":\"破损退货\",\"A03\":\"退货补陈列\",\"A04\":\"退货补盘亏\",\"A05\":\"产品质量退货\",\"A06\":\"退货补赔偿\",\"A07\":\"锁码错码退货\",\"A08\":\"临期品退货\",\"A09\":\"大库存商品退货\",\"A10\":\"退货转试饮品\",\"A11\":\"户外销售退货\",\"A12\":\"其他原因退货\",\"A13\":\"奶卡兑换\",\"A14\":\"补残\",\"A15\":\"物流退货(POD退货)\",\"A16\":\"其他补货\",\"Y98\":\"外围系统退货请求\",\"Y99\":\"原材料/废品/其他等销售\",\"Z1\":\"蒙牛低温\",\"Z2\":\"达能低温\"}},{\"code\":\"refuseReason\",\"groupCode\":\"forecast\",\"name\":\"拒绝原因\",\"map\":{\"10\":\"无货，在途未达\",\"11\":\"无款，款不足\",\"12\":\"无款，欠款\",\"13\":\"无货,保温试验等待放行\",\"14\":\"无货,淘汰产品\",\"15\":\"打折后计划外需求\",\"16\":\"回复量小无法配车\",\"17\":\"实际到货地址与系统到货地址不符\",\"18\":\"非客户需求产品\",\"19\":\"按库房托盘数订单\",\"20\":\"关闭订单\",\"21\":\"改单，零包整改\",\"22\":\"改单，调度无货下单\",\"23\":\"改单，物流装车剩余\",\"24\":\"改单，变换品项\",\"25\":\"改单，调度通知减量\",\"26\":\"改单，调度未按装箱标准下单\",\"27\":\"学生奶口味/包装互调\",\"28\":\"货损\",\"29\":\"部分品项差量控制紧俏品\",\"30\":\"因不卸车缓发原因停货\",\"31\":\"因窜货，要求停发\",\"32\":\"超旬回复量\",\"33\":\"小品项等待生产\",\"34\":\"BP无货等生产\",\"35\":\"BP无货在途\",\"36\":\"BP产品待检\",\"37\":\"BP不足底线车型\",\"38\":\"BP大区要求停发\",\"39\":\"BP无计划或超计划需求\",\"40\":\"BP无款、货款不足\",\"41\":\"BP实际到货地址与系统到货地址不符\",\"42\":\"BP客户录错、重复预报\",\"43\":\"BP关闭未清销售订单\",\"44\":\"BP产品已下市淘汰\",\"45\":\"BP客户要求等通知调单\",\"46\":\"BP产品生产日期不符合客户需求\",\"01\":\"不够发运标准\",\"02\":\"不足起订单\",\"03\":\"大区要求停发\",\"04\":\"计划外需求\",\"05\":\"客户要求作废，录错订单\",\"06\":\"客户要求作废，日期陈旧\",\"07\":\"客户要求作废，挑产地\",\"08\":\"客户要求作废，重复预报订单\",\"09\":\"无货，等待生产\",\"Y0\":\"YSL供应链-产品大日期\",\"Y1\":\"YSL供应链-月中晚供\",\"Y2\":\"YSL销售-超过截单日\",\"Y3\":\"YSL销售-订单错误，客户要求重录\",\"Y4\":\"YSL销售-政策原因，改单\",\"Y5\":\"YSL销售-不够发运标准\",\"Y6\":\"YSL销售-差款、等银行贷款\",\"Y7\":\"YSL供应链-超计划量作废\",\"Y8\":\"YSL销售-客户冻结\",\"Y9\":\"YSL销售-产品下市\",\"Z1\":\"销售公司：关闭行项目\",\"Z2\":\"销售公司：缺货\"}},{\"code\":\"reverseType\",\"groupCode\":\"saleOrder\",\"name\":\"退货类型\",\"map\":{\"ZRE2\":\"标准退货订单\",\"ZRE3\":\"物流退货订单\"}},{\"code\":\"exchangeType\",\"groupCode\":\"saleOrder\",\"name\":\"换货类型\",\"map\":{\"ZCH2\":\"换货订单\"}},{\"code\":\"analyReason\",\"groupCode\":\"forecast\",\"name\":\"分析原因\",\"map\":{\"11\":\"账款不足\",\"01\":\"库存不足\",\"03\":\"超活动量\",\"04\":\"客户挑日期\",\"02\":\"不足起运量\",\"07\":\"其他\",\"05\":\"客户要求作废\",\"06\":\"完全满足\"}}]";
        String text = "[{\"code\":\"analyReason\",\"groupCode\":\"forecast\",\"name\":\"分析原因\",\"map\":{\"11\":\"账款不足\",\"01\":\"库存不足\",\"03\":\"超活动量\",\"04\":\"客户挑日期\",\"02\":\"不足起运量\",\"07\":\"其他\",\"05\":\"客户要求作废\",\"06\":\"完全满足\"}}]";
        if (StringUtils.isNotBlank(enums)){
            text = enums;
        }
        try {
            List<CreateTest> list = JSON.parseArray(text1, CreateTest.class);
            List<CreateDictionaryRequest> requests = convertList(list);
            requests.forEach(request1 -> {
                dictionarySaveService.createDict(request1);
            });
            System.out.println("1111");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static List<CreateDictionaryRequest> convertList(List<CreateTest> createTestList) {
        return createTestList.stream().map(TestController::convert).collect(Collectors.toList());
    }

    private static CreateDictionaryRequest convert(CreateTest createTest) {
        CreateDictionaryRequest request = new CreateDictionaryRequest();
        request.setCode(createTest.getCode());
        request.setName(createTest.getName());
        request.setDesc(createTest.getName());
        request.setGroupCode(createTest.getGroupCode());
        request.setCreatorId("CZ_275369625348335355");
        request.setCreatorName("黔雷");
        request.setModifierId("CZ_275369625348335355");
        request.setModifierName("黔雷");
        request.setGmtCreate(new Date());
        request.setGmtModified(new Date());
//        request.setFeatures();
        List<CreateDictValueRequest> dictValueList = new ArrayList<>();
        createTest.getMap().forEach((k, v) -> {
            CreateDictValueRequest valueRequest = new CreateDictValueRequest();
            valueRequest.setValueData(k);
            valueRequest.setValueName(v);
            valueRequest.setDictCode(createTest.getCode());
            valueRequest.setDesc(v);
            valueRequest.setPriority(1);
            valueRequest.setCreatorId("CZ_275369625348335355");
            valueRequest.setCreatorName("黔雷");
            valueRequest.setModifierId("CZ_275369625348335355");
            valueRequest.setModifierName("黔雷");
            valueRequest.setGmtCreate(new Date());
            valueRequest.setGmtModified(new Date());
//            valueRequest.setFeatures();
            dictValueList.add(valueRequest);

        });
        request.setDictValueList(dictValueList);
        return request;
    }

    public static void main(String[] args) {

        String text = "[{\"code\":\"forecastTag\",\"name\":\"预报单类型\",\"map\":{\"01\":\"标准预报订单\",\"02\":\"车销预报订单\",\"03\":\"补货预报订单\",\"04\":\"共享仓直发客户预报订单\"}},{\"code\":\"saleOrderTag\",\"name\":\"销售单类型\",\"map\":{\"ZOR2\":\"标准销售订单\",\"ZFD2\":\"免费销售订单\",\"ZXC2\":\"车销销售订单\",\"ZFD3\":\"补货销售订单\",\"ZBV\":\"现金销售订单\"}},{\"code\":\"orderReason\",\"name\":\"单据原因\",\"map\":{\"001\":\"客户加单\",\"002\":\"奶酪订单-内销订单\",\"003\":\"按正常销售发放试饮品\",\"004\":\"客户退货\",\"005\":\"价格主数据维护有误\",\"006\":\"货损\",\"007\":\"买赠\",\"A01\":\"过期品退货\",\"A02\":\"破损退货\",\"A03\":\"退货补陈列\",\"A04\":\"退货补盘亏\",\"A05\":\"产品质量退货\",\"A06\":\"退货补赔偿\",\"A07\":\"锁码错码退货\",\"A08\":\"临期品退货\",\"A09\":\"大库存商品退货\",\"A10\":\"退货转试饮品\",\"A11\":\"户外销售退货\",\"A12\":\"其他原因退货\",\"A13\":\"奶卡兑换\",\"A14\":\"补残\",\"A15\":\"物流退货(POD退货)\",\"A16\":\"其他补货\",\"Y98\":\"外围系统退货请求\",\"Y99\":\"原材料/废品/其他等销售\",\"Z1\":\"蒙牛低温\",\"Z2\":\"达能低温\"}},{\"code\":\"refuseReason\",\"name\":\"拒绝原因\",\"map\":{\"ZOR2\":\"标准销售订单\",\"ZFD2\":\"免费销售订单\",\"ZXC2\":\"车销销售订单\",\"ZFD3\":\"补货销售订单\",\"ZBV\":\"现金销售订单\"}},{\"code\":\"reverseType\",\"name\":\"退货类型\",\"map\":{\"ZRE2\":\"标准退货订单\",\"ZRE3\":\"物流退货订单\"}},{\"code\":\"exchangeType\",\"name\":\"换货类型\",\"map\":{\"ZCH2\":\"换货订单\"}}]";

        try {
//            CreateDictionaryRequest request = new CreateDictionaryRequest();
            List<CreateTest> list = JSON.parseArray(text, CreateTest.class);
            List<CreateDictionaryRequest> requests = convertList(list);
            System.out.println(JSON.toJSONString(requests));
//            requests.forEach(request1 -> {
//                dictionarySaveService.createDict(request1);
//            });
            System.out.println("1111");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
